package com.freak.neteasecloudmusic.modules.video.fragment;

import com.freak.httphelper.ApiCallback;
import com.freak.httphelper.HttpMethods;
import com.freak.httphelper.RxPresenter;
import com.freak.httphelper.SubscriberCallBack;
import com.freak.neteasecloudmusic.app.api.ApiService;
import com.freak.neteasecloudmusic.commom.constants.Constants;
import com.freak.neteasecloudmusic.modules.video.entity.VideoEntity;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.freak.neteasecloudmusic.utils.ToastUtil;

import io.reactivex.Observable;

/**
 * Created by Freak on 2020/3/7.
 */
public class VideoTabPresenter extends RxPresenter<VideoTabContract.View> implements VideoTabContract.Presenter {
    private ApiService mApiService = HttpMethods.getInstance().create(ApiService.class);

    @Override
    public void getVideo(String id) {
//        Observable<Object> observable = mApiService.getVideo(id);
//        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
//            @Override
//            public void onSuccess(Object model) {
//                LogUtil.e(new Gson().toJson(model));
//            }
//
//            @Override
//            public void onFailure(String msg) {
//                mView.showToast(msg);
//            }
//        }));
        Observable<VideoEntity> observable = mApiService.getVideo(id);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<VideoEntity>() {
            @Override
            public void onSuccess(VideoEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getVideoSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }
}
